package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplus.mapper.ConsultMergeMapper;
import com.example.mybatisplus.model.domain.ConsultMerge;
import com.example.mybatisplus.model.domain.SingleTime;
import com.example.mybatisplus.service.ConsultMergeService;
import com.example.mybatisplus.service.SingleTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2022-05-31
 */
@Service
public class ConsultMergeServiceImpl extends ServiceImpl<ConsultMergeMapper, ConsultMerge> implements ConsultMergeService {
    @Autowired
    ConsultMergeMapper consultMergeMapper;

    @Autowired
    private SingleTimeService singleTimeService;

    @Override
    public boolean queryConsult(Long stuId) {
        String queryConsult = consultMergeMapper.queryConsult(stuId);
        return queryConsult != null;
    }

    @Override
    public void addConsult(ConsultMerge consultMerge) {

        Date startdate = getStarttime(consultMerge.getTimeId());

        consultMerge.setConsultCount(8);

        Calendar endClender = Calendar.getInstance();
        endClender.setTime(startdate);
        endClender.add(Calendar.DATE,7*8);
        Date endDate = endClender.getTime();

        consultMerge.setStartDate(startdate);

//        consultMerge.setEndDate(consultMerge.getStartDate());
        
        consultMerge.setEndDate(endDate);
        consultMergeMapper.addConsult(consultMerge);

        autoaddandupdate(consultMerge);

    }

    @Override
    public String updateconsultMerge(ConsultMerge consultMerge) {
        if (consultMergeMapper.getMergeById(consultMerge.getStuId())==null){
            return "查询失败，请创建记录";
        }
        else if(isbrokeofmerge(consultMerge)==false){
            return "更新失败，请选择其它咨询师或其它时间段";
        }
        else {
            consultMergeMapper.updateconsultMerge(consultMerge);

            autoaddandupdate(consultMerge);

            return "更新成功";
        }
    }

    @Override
    public List<ConsultMerge> getall() {
        return consultMergeMapper.getall();
    }

    @Override
    public void deletebyId(Long id) {
        consultMergeMapper.deletelogic(id);
    }

    @Override
    public Date getStarttime(Long id) {
        SingleTime singleTime = singleTimeService.getById(id);
        int start = 0;
        int end = 0;
        if (singleTime.getTimeDay().length() == 9) {
            start = Integer.parseInt(singleTime.getTimeDay().substring(0, 1));
            end = Integer.parseInt(singleTime.getTimeDay().substring(5, 6));
            System.out.println(start);
            System.out.println(end);
        }
        if (singleTime.getTimeDay().length() == 10) {
            start = Integer.parseInt(singleTime.getTimeDay().substring(0, 1));
            end = Integer.parseInt(singleTime.getTimeDay().substring(5, 7));
            System.out.println(start);
            System.out.println(end);
//            start = Integer.parseInt(singleTime.getTimeDay().substring(0,1));
        }
        if (singleTime.getTimeDay().length() == 11) {
            start = Integer.parseInt(singleTime.getTimeDay().substring(0, 2));
            end = Integer.parseInt(singleTime.getTimeDay().substring(6, 8));
            System.out.println(start);
            System.out.println(end);
//            start = Integer.parseInt(singleTime.getTimeDay().substring(0,1));
        }
        if(end == 12){
            end = 0;
        }
        int Weekday = 0;
        if (singleTime.getWeakDay().equals("周日")) {
            Weekday = 1;
        } else if (singleTime.getWeakDay().equals("周一")) {
            Weekday = 2;
        } else if (singleTime.getWeakDay().equals("周二")) {
            Weekday = 3;
        } else if (singleTime.getWeakDay().equals("周三")) {
            Weekday = 4;
        } else if (singleTime.getWeakDay().equals("周四")) {
            Weekday = 5;
        } else if (singleTime.getWeakDay().equals("周五")) {
            Weekday = 6;
        } else if (singleTime.getWeakDay().equals("周六")) {
            Weekday = 7;
        }
        System.out.println("Weekday:" + Weekday + " 1" + singleTime.getWeakDay() + "1");


        System.out.println("#######################");
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        startDate.setTime(new Date());
        endDate.setTime(new Date());
        System.out.println("当前是周" + startDate.get(Calendar.DAY_OF_WEEK));
        if (startDate.get(Calendar.DAY_OF_WEEK) > Weekday && Weekday != 1) {
            startDate.add(Calendar.DATE, 7-(startDate.get(Calendar.DAY_OF_WEEK)-Weekday));
            endDate.add(Calendar.DATE, 7-(startDate.get(Calendar.DAY_OF_WEEK)-Weekday));
        } else if (startDate.get(Calendar.DAY_OF_WEEK) == Weekday){
            System.out.println(Calendar.HOUR);
            int tempHour = calendar.get(Calendar.HOUR);
            int tempMinute = calendar.get(Calendar.MINUTE);
            if(start<=tempHour&&tempHour<=end){
                if(end == tempHour){
                    if(tempMinute == 0){

                    } else {
                        startDate.add(Calendar.DATE, 7);
                        endDate.add(Calendar.DATE, 7);
                    }
                } else {
                    startDate.add(Calendar.DATE, 7);
                    endDate.add(Calendar.DATE, 7);
                }
            } else {
                startDate.add(Calendar.DATE, 7);
                endDate.add(Calendar.DATE, 7);
            }
        }
        endDate.set(Calendar.DAY_OF_WEEK, Weekday);
        endDate.set(Calendar.HOUR, end);
        endDate.set(Calendar.MINUTE, 0);
        endDate.set(Calendar.SECOND, 0);
        startDate.set(Calendar.DAY_OF_WEEK, Weekday);
        startDate.set(Calendar.HOUR, start);
        startDate.set(Calendar.MINUTE, 0);
        startDate.set(Calendar.SECOND, 0);
        System.out.println("开始时间:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(startDate.getTime()));
        System.out.println("结束时间:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(endDate.getTime()));
        System.out.println("#######################");
        Date startdate = startDate.getTime();
        return startdate;
    }

    @Override
    public List<ConsultMerge> queryConsultMerge(Long stuId) {
        List<ConsultMerge> consultMerges = consultMergeMapper.queryConsultMerge(stuId);
        return consultMerges;
    }

    private boolean isbroken(ConsultMerge consultMerge){
        if (consultMerge.getTimeId()!=null){
            return true;
        }
        else return false;
    }

    private boolean issample(ConsultMerge consultMerge){
        if(consultMergeMapper.getsampleinrecordlog(consultMerge.getTeaId(),consultMerge.getStuId(),consultMerge.getTimeId())==null){
            return true;
        }
        else return false;
    }

    public void autoaddandupdate(ConsultMerge consultMerge){
        if(issample(consultMerge)==true && consultMergeMapper.getById(consultMerge.getStuId())==consultMerge.getStuId()){
            for (int i=1 ;i<=8;i++){
                consultMerge.setConsultCount(i);
                consultMergeMapper.autoupdate(consultMerge.getStuId(),consultMerge.getTeaId(),consultMerge.getTimeId(),consultMerge.getConsultCount());
            }
        }
        else if (isbroken(consultMerge)==true && issample(consultMerge)==true && consultMergeMapper.getById(consultMerge.getStuId())!=consultMerge.getStuId()){
            for(int i=1;i<=8;i++){
                consultMerge.setConsultCount(i);
                consultMergeMapper.autoadd(consultMerge.getStuId(),consultMerge.getTeaId(),consultMerge.getTimeId(),consultMerge.getConsultCount());
            }
        }
        else {
        }
    }

    public boolean isbrokeofmerge(ConsultMerge consultMerge){
        if(consultMergeMapper.isbrokeofmerge(consultMerge.getTimeId(),consultMerge.getTeaId())==null){
            return true;
        }
        else return false;
    }



}
